//
//  PictureSelectCell.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/12/4.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "PictureSelectCell.h"

@implementation PictureSelectCell

- (void)setImage:(UIImage *)image{
    _image = image;
    if (image == nil) {
        self.deleteBtn.hidden = YES;
    }else{
        self.deleteBtn.hidden = NO;
    }

    [self.addBtn setImage:image forState:UIControlStateNormal];
}
- (void)addBtnDidClick;{
//    防止点击添加的图片时再次添加图片的bug
    if(self.image != nil){
        
        return;
    }
    
    if([self.delegate respondsToSelector:@selector(userWillChosePicture)]){
        [self.delegate userWillChosePicture];
    }
    
}
- (void)deleteBtnDidClick;{
    if([self.delegate respondsToSelector:@selector(userWillDeletePicture:)]){
        [self.delegate userWillDeletePicture:self];
    }
}

//重写构造方法
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self setupUI];
    return self;
}

- (void)setupUI{
    UIButton *addbtn = [[UIButton_Extesion alloc]initWithBackImageName:@"compose_pic_add" imageName:nil];
    UIButton *deleteBtn = [[UIButton_Extesion alloc]initWithBackImageName:nil imageName:@"compose_photo_close"];
    /*
     UIViewContentModeScaleToFill,
     UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
     UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
     UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
     UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,

     */
  //设置按钮的视图显示样式  本身知识按钮的显示样式  但是真正起作用的是按钮内部的imageView在显示图片
    addbtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.addBtn = addbtn;
    self.deleteBtn = deleteBtn;
    [self.addBtn addTarget:self action:@selector(addBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteBtn addTarget:self action:@selector(deleteBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_addBtn];
    [self.contentView addSubview:_deleteBtn];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(_addBtn);
    }];
}
@end
